<template>
  <div :class="className" :style="{height:height,width:width}"/>
</template>

<script>
import echarts from 'echarts'

require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '300px'
    }
  },
  data() {
    return {
      chart: null
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      const labelTop = {
        normal: {
          label: {
            show: true,
            position: 'center',
            formatter: '{b}',
            textStyle: {
              baseline: 'bottom'
            }
          },
          labelLine: {
            show: false
          }
        }
      }
      const labelFromatter = {
        normal: {
          label: {
            formatter: function(params) {
              return 100 - params.value + '%'
            },
            textStyle: {
              baseline: 'top'
            }
          }
        }
      }
      var labelBottom = {
        normal: {
          color: '#ccc',
          label: {
            show: true,
            position: 'center'
          },
          labelLine: {
            show: false
          }
        },
        emphasis: {
          color: 'rgba(0,0,0,0)'
        }
      }
      const radius = [40, 55]
      this.chart = echarts.init(this.$el, 'macarons')

      this.chart.setOption({
        tooltip: {
          trigger: 'item',
          formatter: '{a} <br/>{b} : {c} ({d}%)'
        },
        legend: {
          x: 'center',
          y: 'center',
          data: [
            '产线负载图', 'Facebook', 'Youtube', 'Google+', 'Weixin',
            'Twitter', 'Skype', 'Messenger', 'Whatsapp', 'Instagram'
          ]
        },
        title: {
          text: 'The App World',
          // subtext: 'from global web index',
          x: 'center'
        },
        series: [
          {
            type: 'pie',
            // center: ['10%', '30%'],
            center: ['50%', '40%'],
            radius: radius,
            x: '0%', // for funnel
            itemStyle: labelFromatter,
            data: [
              { name: 'other', value: 46, itemStyle: labelBottom },
              { name: 'GoogleMaps', value: 54, itemStyle: labelTop }
            ]
          }]
      })
    }
  }
}
</script>
